from datetime import datetime
from app import db

class DecorationRequest(db.Model):
    """装修请求模型"""
    __tablename__ = 'decoration_requests'
    
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False, index=True)
    conversation_name = db.Column(db.String(200), nullable=False)
    project_name = db.Column(db.String(200), nullable=False)
    
    # 用户输入参数
    decoration_style = db.Column(db.String(50), nullable=False)
    room_type = db.Column(db.String(50), nullable=False)
    decoration_plan = db.Column(db.Text, nullable=False)
    budget_min = db.Column(db.Integer, nullable=False)
    budget_max = db.Column(db.Integer, nullable=False)
    
    # 文件信息
    floor_plan_url = db.Column(db.String(500), nullable=True)
    
    # 请求状态
    status = db.Column(db.String(20), default='pending')  # pending, processing, completed, failed
    
    # 响应数据
    coze_response = db.Column(db.Text, nullable=True)
    generated_images = db.Column(db.Text, nullable=True)  # JSON格式存储图片URL列表
    
    # 时间戳
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 关系定义
    uploaded_files = db.relationship('UploadedFile', backref='decoration_request', lazy=True, cascade='all, delete-orphan')
    
    def to_dict(self):
        """转换为字典格式"""
        return {
            'id': self.id,
            'user_id': self.user_id,
            'conversation_name': self.conversation_name,
            'project_name': self.project_name,
            'decoration_style': self.decoration_style,
            'room_type': self.room_type,
            'decoration_plan': self.decoration_plan,
            'budget_min': self.budget_min,
            'budget_max': self.budget_max,
            'floor_plan_url': self.floor_plan_url,
            'status': self.status,
            'generated_images': self.generated_images,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        }
    
    def __repr__(self):
        return f'<DecorationRequest {self.id}: {self.project_name}>'
